package com.naiterui.ehp.bs.doctor.dao.impl;

import com.naiterui.ehp.bp.domain.ConsultSession;
import com.naiterui.ehp.bs.doctor.dao.IConsultSessionDao;
import com.naiterui.ehp.bs.doctor.repository.IConsultSessionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class AdConsultSessionDao implements IConsultSessionDao {

    @Autowired
    private IConsultSessionRepository consultSessionRepository;

    @Override
    public void save(ConsultSession consultSession) {
        consultSessionRepository.save(consultSession);
    }

    @Override
    public void update(ConsultSession consultSession) {
        consultSessionRepository.update(consultSession);
    }

    @Override
    public ConsultSession getBySessionId(String sessionId) {
        List<ConsultSession> resultList = consultSessionRepository.findBySessionId(sessionId);
        if (null != resultList && !resultList.isEmpty()) {
            return resultList.get(0);
        }
        return null;
    }

    @Override
    public void updateSessionRecomFlag(Long id) {
        ConsultSession session = consultSessionRepository.get(id);
        session.setRecomDrugFlag(ConsultSession.HAVE_RECOM);
        consultSessionRepository.update(session);
    }

    @Override
    public List<ConsultSession> getConsultSession(Long doctorId, List<Long> patientIds, Date date) {
        return consultSessionRepository.findByDoctorIdAndPatientIdInAndStartTimeGreaterThanEqual(doctorId, patientIds, date);
    }

    @Override
    public List<ConsultSession> getConsultSession(Long doctorId, Date timeGt, Date timeLt) {
        return consultSessionRepository.findByDoctorIdAndStartTimeGreaterThanAndStartTimeLessThanEqual(doctorId, timeGt, timeLt);
    }

    @Override
    public ConsultSession findConsultSessionByDoctorIdAndPatientIdAndInquirerId(Long doctorId, Long patientId, Long inquirerId) {
        return consultSessionRepository.findFirstByDoctorIdAndPatientIdAndInquirerIdAndEndTimeIsNullOrderByStartTimeDesc(doctorId, patientId, inquirerId);
    }

}
